package com.zmc.dynamic.util;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.IdUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Cassius
 * @date 2019/9/4 13:24
 */
public class IdUtils extends IdUtil {

    public static String simpleUUID(String prefix) {
        return prefix + UUID.randomUUID().toString(true);
    }

    public static String simpleUUID(String prefix,String suffix) {
        return prefix + UUID.randomUUID().toString(true) + "_" + suffix;
    }

    public static String traceID(String prefix) {
        StringBuffer sb = new StringBuffer(20);
        sb.append(MD5Util.encrypt16(UUID.randomUUID().toString(true)).toUpperCase());
        return prefix + MD5Util.encrypt16(UUID.randomUUID().toString(true)) + "_" + sb.toString();
    }

    public synchronized static String getKeyId() {
        StringBuffer sb = new StringBuffer(30);
        sb.append(DateUtil.getDateTimeNum(new Date()));
        sb.append(MD5Util.encrypt16(UUID.randomUUID().toString(true)).toUpperCase());
        return sb.toString();
    }

    public static String getKeyId(String prefix) {
        return prefix + getKeyId();
    }

    public static String getKey(String... param) {
        return getKey(param, ":");
    }

    public static String getKey(String[] param, String separator) {
        List<String> list = new ArrayList<>();
        for (String str : param) {
            if(StringUtil.isNotEmpty(str)){
                list.add(str);
            }
        }
        if(CollectionUtils.isEmpty(list)){
            throw new RuntimeException("获取key失败,参数不能为空!");
        }
        return StringUtils.join(list, separator);
    }

}
